﻿package frEngine.core
{
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.system.*;
    import baseEngine.utils.*;
    import flash.geom.*;
    import frEngine.core.lenses.*;
    import frEngine.math.*;

    public class OrthographicCamera3D extends Camera3D
    {
        private var _lens:OrthographicLens;
        private var _tempPos:Vector3D;
        private var _direction:Vector3D;
        private static var _inv:Matrix3D;

        public function OrthographicCamera3D(param1:String = "", param2:Number = 75, param3:Number = 1, param4:Number = 10000)
        {
            this._tempPos = new Vector3D();
            this._direction = new Vector3D();
            this._lens = new OrthographicLens();
            super(param1, param2, this._lens, param3, param4);
            this._lens.far = param4;
            return;
        }// end function

        public function setSceenScale(param1:Number, param2:Number) : void
        {
            this._lens.setScale(param1, param2);
            return;
        }// end function

        override public function updateProj(param1:Rectangle, param2:Matrix3D) : void
        {
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:* = this.near;
            var _loc_6:* = this.far;
            var _loc_7:* = this.aspectRatio;
            var _loc_8:* = this.scene || Global3D.scene;
            if (param1)
            {
                _loc_3 = param1.width;
                _loc_4 = param1.height;
            }
            else
            {
                if (!_loc_8 || !_loc_8.viewPort)
                {
                    return;
                }
                _loc_3 = _loc_8.viewPort.width;
                _loc_4 = _loc_8.viewPort.height;
            }
            if (isNaN(_loc_7))
            {
                _loc_7 = _loc_3 / _loc_4;
            }
            this._lens.projectionHeight = _loc_4 / this.zoom;
            this._lens.aspectRatio = _loc_7;
            this._lens.far = this.far;
            param2.copyFrom(this._lens.matrix);
            return;
        }// end function

        override public function clone() : Pivot3D
        {
            var _loc_1:Pivot3D = null;
            var _loc_2:* = new OrthographicCamera3D(name);
            _loc_2.copyFrom(this);
            _loc_2.near = this.near;
            _loc_2.far = this.far;
            _loc_2.fieldOfView = this.fieldOfView;
            for each (_loc_1 in children)
            {
                
                if (!_loc_1.lock)
                {
                    _loc_2.addChild(_loc_1.clone());
                }
            }
            return _loc_2;
        }// end function

        override public function screenToCamera(param1:Number, param2:Number, param3:Number, param4:Vector3D) : void
        {
            param4.x = param1;
            param4.y = param2;
            param4.z = param3 * (this.far - this.near) + this.near;
            return;
        }// end function

        override public function getPointDir(param1:Number, param2:Number, param3:Vector3D = null) : Vector3D
        {
            if (!param3)
            {
                param3 = new Vector3D();
            }
            this.getDir(false, this._direction);
            param3.x = this._direction.x;
            param3.y = this._direction.y;
            param3.z = this._direction.z;
            return param3;
        }// end function

        override public function getScreenMouseDistance(param1:Number, param2:Number, param3:Vector3D) : Number
        {
            var _loc_4:* = Global3D.scene.viewPort;
            if (!_loc_4)
            {
                return Number.MAX_VALUE;
            }
            param1 = param1 - _loc_4.x;
            param2 = param2 - _loc_4.y;
            this._tempPos.x = (param1 / _loc_4.width - 0.5) * 2;
            this._tempPos.y = ((-param2) / _loc_4.height + 0.5) * 2;
            this._tempPos.z = 1;
            _inv = _inv || new Matrix3D();
            _inv.copyFrom(this.projection);
            _inv.invert();
            Matrix3DUtils.transformVector(_inv, this._tempPos, this._tempPos);
            Matrix3DUtils.transformVector(world, this._tempPos, this._tempPos);
            getDir(false, this._direction);
            return FrMathUtil.pointToLineDistance2(param3, this._tempPos, this._direction);
        }// end function

    }
}
